// @bfmeta/node-sdk
const mysql = require('mysql2');
const { BFMetaSDK } = require ("@bfmeta/node-sdk")
const { CryptoHelper} =  require ("@bfmeta/node-sdk/build/test/helpers/cryptoHelper");
const {FecthDemo} = require("@bfmeta/node-sdk/build/test/helpers/fecthDemo")
const multer = require('multer')

// 创建SDK配置
const bfmetaConfig = {
  node: {
      /**节点 ip, 默认值 [127.0.0.1] */
      ip: "34.84.140.206",
      /**节点端口号, 默认值 9003 */
      port: 19003,
  },
  //  "请求超时时间, 单位 ms, 默认 10000",
  requestTimeOut: 10000,
  // "请求协议, http || websocket, 默认值 websocket",
  requestProtocol: 'websocket',
}
// 创建数据库连接池配置
const mysqlConfig = {
  host: 'rm-bp1035514z9uq43q46o.mysql.rds.aliyuncs.com', // 数据库主机名
  port: 3306, //端口
  user: 'admin_1066365803', // 数据库用户名
  password: '142536aA', // 数据库密码
  database: 'web3-vote' // 数据库名
}

const cryptoHelper = new CryptoHelper();
const fecthDemo = new FecthDemo();
// 生成SDK实例
const bfmetaSDK = new BFMetaSDK(bfmetaConfig, { netType: "testnet", cryptoHelper}, fecthDemo);
// 生成数据库连接实例
const db = mysql.createPool(mysqlConfig).promise()

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'public/img')
  },
  filename: (request, file, callback) => {
    let fileExtension = file.originalname.split('.')[1];
    if(!fileExtension) fileExtension = ''
    const newNameFile = require('crypto').randomBytes(16).toString('hex')+'.png';

    callback(null, `${fileExtension}${newNameFile}`)
  }
});
const upload = multer({ storage });

// db.execute(
//   "select * from user"
// ).then((data) => {
//   console.log(data)
// })


module.exports = {bfmetaSDK, db, upload}